﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Buy
namespace backoffice.Data
{
    partial class Buy
    {
        public void setBuy(Buy b)
        {
            this.BuyID = b.BuyID;
            this.UserID = b.UserID;
            this.RegionID = b.RegionID;
            this.PlantID = b.PlantID;
            this.Offer = b.Offer;
            this.Currency = b.Currency;
            this.Date = b.Date;
        }
    }
}
#endregion

#region dataBuy
namespace backoffice.Data
{
    public class dataBuy
    {
        #region Create
        public static void createBuy(Buy b)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                b.BuyID = Guid.NewGuid();
                b.Date = new DateTime();

                db.Buys.InsertOnSubmit(b);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Buy getBuy(Guid guid)
        {
            try
            {
                if (guid == null)
                {
                    return null;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    return db.Buys.FirstOrDefault(buy => buy.BuyID == guid);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Buy> getBuys(Expression<Func<Buy, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Buys.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeBuy(Buy b)
        {
            try
            {
                if (b == null)
                {
                    return false;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Buy res = db.Buys.FirstOrDefault(buy => buy.BuyID == b.BuyID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        res.setBuy(b);

                        db.SubmitChanges();

                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteBuy(Buy b)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Buys.Contains(b))
                {
                    Buy res = db.Buys.FirstOrDefault(buy => buy.BuyID == b.BuyID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        db.Buys.DeleteOnSubmit(res);
                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion